package com.witframework.web.tag;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class AjaxPanelTag extends BodyTagSupport{
  private static final String AJAXPANEL = "jxnpnlcj2006";
  private String url = null;
  private String urlFunction = null;
  private String error = null;
  private boolean visible = false;
  private String openedImage = "/webresource/images/opentriangle.gif";
  private String closedImage = "/webresource/images/triangle.gif";
  private String idPanel = null;
  private String style = null;
  private String className = null;
  private String beforeAction = null;
  private String afterAction = null;
  private String sBody = null;

  public void setBeforeAction(String paramString)
  {
    this.beforeAction = paramString;
  }

  public String getBeforeAction()
  {
    return this.beforeAction;
  }

  public void setAfterAction(String paramString)
  {
    this.afterAction = paramString;
  }

  public String getAfterAction()
  {
    return this.afterAction;
  }

  public void setUrl(String paramString)
  {
    this.url = paramString;
  }

  public String getUrl()
  {
    return this.url;
  }

  public void setUrlFunction(String paramString)
  {
    this.urlFunction = paramString;
  }

  public String getUrlFunction()
  {
    return this.urlFunction;
  }

  public void setError(String paramString)
  {
    this.error = paramString;
  }

  public String getError()
  {
    return this.error;
  }

  public void setVisible(boolean paramBoolean)
  {
    this.visible = paramBoolean;
  }

  public boolean getVisible()
  {
    return this.visible;
  }

  public void setOpenedImage(String paramString)
  {
    this.openedImage = paramString;
  }

  public String getOpenedImage()
  {
    return this.openedImage;
  }

  public void setClosedImage(String paramString)
  {
    this.closedImage = paramString;
  }

  public String getClosedImage()
  {
    return this.closedImage;
  }

  public void setIdPanel(String paramString)
  {
    this.idPanel = paramString;
  }

  public String getIdPanel()
  {
    return this.idPanel;
  }

  public void setStyle(String paramString)
  {
    this.style = paramString;
  }

  public String getStyle()
  {
    return this.style;
  }

  public void setClassName(String paramString)
  {
    this.className = paramString;
  }

  public String getClassName()
  {
    return this.className;
  }

  public int doAfterBody()
    throws JspException
  {
    BodyContent localBodyContent = getBodyContent();
    if (localBodyContent != null)
    {
      this.sBody = localBodyContent.getString();
      localBodyContent.clearBody();
    }
    if (this.sBody == null)
      this.sBody = "";
    else
      this.sBody = this.sBody.trim();
    return 0;
  }

  public int doEndTag() throws JspException {
	HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
    StringBuffer localStringBuffer = new StringBuffer("");
    int i = -1;
    Integer localInteger = (Integer)this.pageContext.getAttribute("jxnpnlcj2006", 1);
    if (localInteger == null)
      i = 0;
    else
      i = localInteger.intValue();
    if (this.idPanel == null)
      this.idPanel = "jxnpnlcj2006" + i;
    this.pageContext.setAttribute("jxnpnlcj2006", new Integer(i + 1), 1);
    localStringBuffer.append(getJavaScript(this.idPanel));
    localStringBuffer.append("<a href=\"javascript:void(0)\" onClick=\"");
    localStringBuffer.append("jxnpnlcj2006_f" + this.idPanel + "('");
    localStringBuffer.append(this.idPanel + "_i','");
    localStringBuffer.append(this.idPanel);
    localStringBuffer.append("','");
    localStringBuffer.append(request.getContextPath()+ this.openedImage);
    localStringBuffer.append("','");
    localStringBuffer.append(request.getContextPath()+ this.closedImage);
    localStringBuffer.append("')\">");
    localStringBuffer.append("<img src=\"");
    if (this.visible)
      localStringBuffer.append(request.getContextPath()+this.openedImage);
    else
      localStringBuffer.append(request.getContextPath()+this.closedImage);
    localStringBuffer.append("\" id=\"");
    localStringBuffer.append(this.idPanel + "_i");
    localStringBuffer.append("\" border=\"0\" alt=\"\">");
    localStringBuffer.append("</a>");
    localStringBuffer.append("&nbsp;");
    localStringBuffer.append(this.sBody);
    if (this.style == null)
      this.style = "";
    else if (!(this.style.endsWith(";")))
      this.style += ";";
    if (this.visible)
      this.style += "display:block";
    else
      this.style += "display:none";
    localStringBuffer.append("\n<span id=\"");
    localStringBuffer.append(this.idPanel);
    localStringBuffer.append("\"");
    if (this.className != null)
    {
      localStringBuffer.append(" class=\"");
      localStringBuffer.append(this.className);
      localStringBuffer.append("\"");
    }
    localStringBuffer.append(" style=\"");
    localStringBuffer.append(this.style);
    localStringBuffer.append("\"");
    localStringBuffer.append(">\n");
    localStringBuffer.append("\n</span>\n");
    if (this.visible)
    {
      localStringBuffer.append("\n<script language=\"JavaScript\" type=\"text/javascript\">\n");
      localStringBuffer.append("cjAjaxEngine(");
      if (this.url != null)
        localStringBuffer.append("'" + this.url + "'");
      else
        localStringBuffer.append(this.urlFunction + "()");
      localStringBuffer.append(",aphandler_" + this.id + ",");
      if (this.error == null)
        localStringBuffer.append("null");
      else
        localStringBuffer.append(this.error);
      if (this.beforeAction == null)
        localStringBuffer.append(",null");
      else
        localStringBuffer.append("," + this.beforeAction);
      if (this.afterAction == null)
        localStringBuffer.append(",null");
      else
        localStringBuffer.append("," + this.afterAction);
      localStringBuffer.append(");");
      localStringBuffer.append("</script>\n");
    }
    try
    {
      this.pageContext.getOut().write(localStringBuffer.toString());
    }
    catch (Exception localException)
    {
      throw new JspException("Could not write data " + localException.toString());
    }
    dropData();
    return 6;
  }

  public void release()
  {
    super.release();
    dropData();
  }

  private void dropData()
  {
    this.url = null;
    this.urlFunction = null;
    this.error = null;
    this.visible = false;
    this.openedImage = "/webresource/images/opentriangle.gif";
    this.closedImage = "/webresource/images/triangle.gif";
    this.idPanel = null;
    this.style = null;
    this.className = null;
    this.sBody = null;
    this.afterAction = null;
    this.beforeAction = null;
  }

  private String getJavaScript(String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer("");
    localStringBuffer.append("\n<script language=\"JavaScript\" type=\"text/javascript\">\n");
    localStringBuffer.append("function aphandler_" + paramString + "(txt,xmlDoc) {");
    localStringBuffer.append("document.getElementById('" + paramString + "').innerHTML=txt;");
    localStringBuffer.append("}\n");
    localStringBuffer.append("function ");
    localStringBuffer.append("jxnpnlcj2006_f" + paramString + "(idImg,idPanel,oImg,cImg) {\n");
    localStringBuffer.append("o=document.getElementById(idImg); s=o.src;");
    localStringBuffer.append("i = s.indexOf(oImg);");
    localStringBuffer.append("if (i<0) {\n");
    localStringBuffer.append("i=s.indexOf(cImg);");
    localStringBuffer.append("if (i==0) o.src=oImg;");
    localStringBuffer.append("else      o.src=s.substring(0,i)+oImg; } ");
    localStringBuffer.append("else   {\n");
    localStringBuffer.append("if (i==0) o.src=cImg;");
    localStringBuffer.append("else      o.src=s.substring(0,i)+cImg;  }");
    localStringBuffer.append("o=document.getElementById(idPanel);\n");
    localStringBuffer.append("if (o.style.display=='none') { o.innerHTML=''; o.style.display='block';");
    localStringBuffer.append("cjAjaxEngine(");
    if (this.url != null)
      localStringBuffer.append("'" + this.url + "'");
    else
      localStringBuffer.append(this.urlFunction + "()");
    localStringBuffer.append(",aphandler_" + paramString + ",");
    if (this.error == null)
      localStringBuffer.append("null");
    else
      localStringBuffer.append(this.error);
    if (this.beforeAction == null)
      localStringBuffer.append(",null");
    else
      localStringBuffer.append("," + this.beforeAction);
    if (this.afterAction == null)
      localStringBuffer.append(",null");
    else
      localStringBuffer.append("," + this.afterAction);
    localStringBuffer.append(");}");
    localStringBuffer.append("else o.style.display='none';");
    localStringBuffer.append("}\n");
    localStringBuffer.append("</script>\n");
    return localStringBuffer.toString();
  }
}